<template>
  <el-scrollbar>
    <div v-for="item in sessions" :key="item" class="scrollbar-demo-item" @click="chat(item)">
      <el-avatar shape="square" :src="url+item.avatar"/>
      <div class="text">{{ item.name }}</div>
      <el-icon style="color: red" @click.stop="deleteChat(item)">
        <CloseBold/>
      </el-icon>
      <div :style="{display:(item.noReadNumber==0?'none':'block')}" class="nav-counter nav-counter-blue">
        {{ item.noReadNumber }}
      </div>
    </div>
  </el-scrollbar>
</template>

<script lang="ts" setup>

import {useRouter} from "vue-router";
import {onMounted, Ref, ref, toRef, toRefs, watch} from "vue";
import {createSession, deleteSession, getSessionVo, setRead} from "../../service/ChatApi";
import {callbacks, MessageDto} from "../../utils/webSocket";
import {error, message, websocketNotify} from "../../utils/messageUtil";

const router = useRouter();
const sessions: Ref<any[]> = ref([])
const style = ref({})

const url = ref(import.meta.env.VITE_APP_BASE_API + "/common/file/loadFileId?fileId=")

const props = defineProps({
  //子组件接收父组件传递过来的值
  session: Object,
})

//监听事件
watch(()=>props.session, (now:any) => {
     getDate()
})

const deleteChat = (session: any) => {
  console.log(router.currentRoute.value.query.sessionId);
  deleteSession({
    acceptId: session.sessionId,
    sessionType: session.sessionType
  }).then(d => {
    if (d.data.code == 200) {
      let number = sessions.value.indexOf(session);
      sessions.value.splice(number, 1);
      //如果当前页面是当前会话
      if(router.currentRoute.value.query.sessionId === session.sessionId){
          router.push({
            name:'center'
          })
      }
    } else {
      error(d.data.message);
    }
  })
}

const getDate = ()=>{
  getSessionVo().then(d => {
    sessions.value = d.data.date
    sessions.value.forEach(item => {
      if (router.currentRoute.value.query.sessionId == item.sessionId) {
       return setRead({
          acceptId: item.sessionId,
          sessionType: item.sessionType
        }).then(d => {
          if (d.data.code == 200) {
            item.noReadNumber = 0;
          }
        })
      }
    })
  })
}

onMounted(() => {
  getDate()
  callbacks.notify = (messageDto: MessageDto) => {
    websocketNotify(messageDto.sendUserSocketDto.name, messageDto.message, messageDto.type)

    let exit: boolean = false;

    sessions.value.forEach(item => {
      if (messageDto.sessionType == 'ALONG') {
        if(messageDto.sendUserSocketDto.userId == item.sessionId){
          exit = true;
        }
      }else{
        if(messageDto.acceptUserSocketDto.userId == item.sessionId){
          exit = true;
        }
      }
    })

    //当前用户并不存在这个会话
    if (!exit) {
      if (messageDto.sessionType == 'ALONG') {
        createSession({
          acceptId: messageDto.sendUserSocketDto.userId,
          sessionType: messageDto.sessionType
        }).then(d => {
          if (d.data.code == 200) {
            sessions.value.unshift(d.data.date)
          }
        })

      } else {
        createSession({
          acceptId: messageDto.acceptUserSocketDto.userId,
          sessionType: messageDto.sessionType
        }).then(d => {
          if (d.data.code == 200) {
            d.data.date.noReadNumber++;
            sessions.value.unshift(d.data.date)
          }
        })
      }
    }

    sessions.value.forEach(item => {
      //不是在当前会话id+1
      if (router.currentRoute.value.query.sessionId != item.sessionId) {
        if (messageDto.sessionType == 'ALONG') {
          if (item.sessionId == messageDto.sendUserSocketDto.userId) {
            item.noReadNumber++;
          }
        }

        if (messageDto.sessionType == 'GROUP') {
          if (item.sessionId == messageDto.acceptUserSocketDto.userId) {
            item.noReadNumber++;
          }
        }
      }
    })
  }
})

const chat = (session: any) => {

  //把之前的读记录也清除
  setRead({
    acceptId: router.currentRoute.value.query.sessionId,
    sessionType: router.currentRoute.value.query.sessionType
  })

  setRead({
    acceptId: session.sessionId,
    sessionType: session.sessionType
  }).then(d => {
    if (d.data.code == 200) {
      sessions.value.forEach(item => {
        if (item.sessionId == session.sessionId) {
          item.noReadNumber = 0;
        }
      })
    }
  })

  router.push({
    name: 'chat',
    query: session
  })
}

</script>

<style scoped>

.nav-counter {
  position: absolute;
  top: 2px;
  right: 3px;
  min-width: 8px;
  height: 20px;
  line-height: 20px;
  margin-top: 10px;
  padding: 0 6px;
  font-weight: normal;
  color: white;
  text-align: center;
  text-shadow: 0 1px rgba(0, 0, 0, 0.2);
  background: #e23442;
  border: 1px solid #911f28;
  border-radius: 11px;
  background-image: -webkit-linear-gradient(top, #e8616c, #dd202f);
  background-image: -moz-linear-gradient(top, #e8616c, #dd202f);
  background-image: -o-linear-gradient(top, #e8616c, #dd202f);
  background-image: linear-gradient(to bottom, #e8616c, #dd202f);
  -webkit-box-shadow: inset 0 0 1px 1px rgba(255, 255, 255, 0.1), 0 1px rgba(0, 0, 0, 0.12);
  box-shadow: inset 0 0 1px 1px rgba(255, 255, 255, 0.1), 0 1px rgba(0, 0, 0, 0.12);
}

.nav-counter-blue {
  background: #e8616c;
  border: 1px solid #dd202f;
  background-image: -webkit-linear-gradient(top, #e8616c, #dd202f);
  background-image: -moz-linear-gradient(top, #e8616c, #dd202f);
  background-image: -o-linear-gradient(top, #e8616c, #dd202f);
  background-image: linear-gradient(to bottom, #e8616c, #dd202f);
}

.scrollbar-demo-item {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 50px;
  margin: 2px;
  text-align: center;
  border-radius: 4px;
  background: var(--el-color-primary-light-9);
  color: var(--el-color-primary);
  position: relative;
}

.text {
  width: 200px;
}
</style>